﻿Imports Abyx.SVCMS.Library.DB
Imports Abyx.SVCMS.Library
Imports Abyx.WindowsExtendedControls

Public Class SVC_Customers_Form
    Implements ICarfChildWindow

#Region "ICARF"
    Public Function GetFormInstanceForDataObject(DataObject As Object) As System.Windows.Forms.Form Implements ICarfChildWindow.GetFormInstanceForDataObject
        Return Me
    End Function
    Public Sub InitializeThisFormAndControlSettings(Optional OrderUpdateToFile As Boolean = False) Implements ICarfChildWindow.InitializeThisFormAndControlSettings
        'init general settings
        AppSettings.InitFormSettings(Me, OrderUpdateToFile)
    End Sub
    Public Sub OrderRefreshRelatedDataControls(Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfChildWindow.OrderRefreshRelatedDataControls
        AppSettings.MainForm.OrderRefreshRelatedDataControls(Me, _
                                             RelatedControlDataCategoriesEnum.Customers, _
                                             DataItem, _
                                             ActionOnDataItem)
    End Sub
    Public Sub ProcessRefreshRelatedDataControls(sender As System.Windows.Forms.Form, DataTypeCategory As RelatedControlDataCategoriesEnum, Optional DataItem As Object = Nothing, Optional ActionOnDataItem As FormActionOnDataEnum = FormActionOnDataEnum.None) Implements ICarfChildWindow.ProcessRefreshRelatedDataControls
        'refresh my own data
        Refresh_CustomersList()
    End Sub
#End Region

#Region "Events"
    Private Sub SVC_Customers_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.InitializeThisFormAndControlSettings()
        Refresh_CustomersList()

    End Sub
    Private Sub SVC_Customers_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.InitializeThisFormAndControlSettings(True)
    End Sub
    Private Sub Search_TextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles Search_TextBox.TextChanged
        Refresh_CustomersList()
    End Sub
    Private Sub New_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles New_ImageButton.Click

        Dim frm As New SVC_CustomerEntry_Form
        frm.MdiParent = AppSettings.MainForm
        frm.Show(FormActionOnDataEnum.NewRecord)

    End Sub
    Private Sub Edit_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Edit_ImageButton.Click

        OpenExistingCustomerRecord

    End Sub
    Private Sub Customers_DetailListBox_DoubleClick(sender As Object, e As System.EventArgs) Handles Customers_DetailListBox.DoubleClick

        OpenExistingCustomerRecord()

    End Sub

    Private Sub Delete_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Delete_ImageButton.Click

        If Customers_DetailListBox.SelectedItem IsNot Nothing Then
            Dim item As DetailListBoxItem = Customers_DetailListBox.SelectedItem
            Dim customer As CustomerType = item.DataObject
            Dim frm As New SVC_CustomerEntry_Form
            frm.MdiParent = AppSettings.MainForm
            frm.Show(FormActionOnDataEnum.DeleteRecord, customer)
        End If

    End Sub
    Private Sub Close_ImageButton_Click(sender As System.Object, e As System.EventArgs) Handles Close_ImageButton.Click
        Me.Close()
    End Sub
#End Region

#Region "Private Procedures"
    Private Sub Refresh_CustomersList()

        Customers_DetailListBox.Items.Clear()
        Dim sm As New SvcManager(AppSettings.LoggedUser)
        Dim customers As CustomerTypeCollection = sm.Get_Customers_Search_COL(Search_TextBox.Text)
        If customers.Count > 0 Then
            For i As Integer = 0 To customers.Count - 1
                Customers_DetailListBox.Items.Add(customers(i).ToDetailsListBoxItem)
            Next
        End If

    End Sub
    Private Sub OpenExistingCustomerRecord()

        If Customers_DetailListBox.SelectedItem IsNot Nothing Then
            Dim item As DetailListBoxItem = Customers_DetailListBox.SelectedItem
            Dim customer As CustomerType = item.DataObject
            Dim frm As New SVC_CustomerEntry_Form
            frm.MdiParent = AppSettings.MainForm
            frm.Show(FormActionOnDataEnum.UpdateRecord, customer)
        End If

    End Sub
#End Region








End Class